//
//  MoreTableViewController.m
//  cityStar
//
//  Created by apple on 14/12/13.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "MoreTableViewController.h"
#import "HTDefine.h"
#import "SideViewController.h"
#import "PeopleCenterTableViewController.h"

#import "HWSearchViewController.h"
#import "LoginCityStarViewViewController.h"
#import "BaseNavController.h"

@interface MoreTableViewController (){
    BOOL isPop;
}

@property (weak, nonatomic) IBOutlet UIButton *btnLogout;

@end

@implementation MoreTableViewController
-(void)viewWillAppear:(BOOL)animated
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=7.0) {
        self.navigationController.navigationBar.translucent = NO; //禁止半透明毛玻璃效果
        self.navigationController.navigationBar.tintColor = kWhiteNavTitleColor; //返回按钮颜色
        self.navigationController.navigationBar.barTintColor = [UIColor whiteColor]; //bar背景颜色
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]}; //标题颜色
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }else{
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; //bar背景颜色
        self.navigationController.navigationBar.translucent = NO;
        self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:kWhiteNavTitleColor,NSFontAttributeName:[UIFont boldSystemFontOfSize:18]};
        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    }
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    isPop = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    self.btnLogout.clipsToBounds = YES;
    self.btnLogout.backgroundColor = rgba(255,196,110,1);
    self.btnLogout.layer.cornerRadius = 5;
    self.tableView.backgroundColor = kBackgroundColor;
    
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    UISwipeGestureRecognizer* recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:recognizer];

}

- (void)initNavBar{
    self.navigationItem.title = @"更多";
    
    UIButton * regBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
    regBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [regBtn setTitleColor:rgba(133, 133, 133, 1) forState:UIControlStateHighlighted];
    [regBtn setImage:[UIImage imageNamed:@"bsearch"] forState:UIControlStateNormal];
    [regBtn setImage:[UIImage imageNamed:@"bsearch_touch"] forState:UIControlStateHighlighted];
    [regBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * iHaveNewsBtnItem = [[UIBarButtonItem alloc]initWithCustomView:regBtn];
    self.navigationItem.rightBarButtonItem = iHaveNewsBtnItem;
    
    UIButton *btnSide = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
    [btnSide setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [btnSide setImage:[UIImage imageNamed:@"more_touch"] forState:UIControlStateHighlighted];
    [btnSide addTarget:self action:@selector(sideMove) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *bbtmLeft = [[UIBarButtonItem alloc]initWithCustomView:btnSide];
    self.navigationItem.leftBarButtonItem = bbtmLeft;
}

- (void)searchAction{
    HWSearchViewController *hwSearchVC = [[HWSearchViewController alloc]init];
    hwSearchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:hwSearchVC animated:YES];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
    [self sideMove];
    //    [self.view removeGestureRecognizer:recognizer];
}

- (void)sideMove{
    [[SideViewController shareVc] showLeftView];
}

- (IBAction)Logout:(id)sender {
    [HttpRequestManager getMethod:KInterFace_logOut parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"saveSection"];
            [SVProgressHUD showSuccessWithStatus:@"成功注销"];
            [[NSUserDefaults standardUserDefaults]removeObjectForKey:KUserDefaultKey_PersonalDetail];
            DLog(@"%@",response);
            [[NSNotificationCenter defaultCenter]postNotificationName:@"LoginCityStarViewSuccess" object:self];
            [self getCitystarHttpToken];
        }else{
            [SVProgressHUD showSuccessWithStatus:@"还未登陆"];

        }
    }];
}

#pragma mark - webRequest
-(void)getCitystarHttpToken
{
    [HttpRequestManager getMethod:KInterface_getToken parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            [[NSUserDefaults standardUserDefaults] setObject:response[@"value"][@"token"] forKey:KUserDefaultsToken];
            [[NSUserDefaults standardUserDefaults] setObject:response[@"value"][@"expires"] forKey:KUserDefaultsexpires];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [[SVHTTPClient sharedClient] setValue:response[@"value"][@"token"] forHTTPHeaderField:@"TOKEN"];
            //            [self webRequest_citydataWithDetailID];
        }
        DLog(@"%@",response);
    }];
}

#pragma mark UITableViewDataSource Methods
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return 15;
    }else if (section == 1) {
        return 10;
    }else if (section == 2) {
        return 10;
    }
    return 0;
}
#pragma mark UITableViewDelegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

    DLog(@"%li,%li",indexPath.section,indexPath.row);
//
    if (indexPath.section == 0 & indexPath.row == 0) {
        NSData *userData = [KStandUserDefaults objectForKey:KUserDefaultKey_PersonalDetail];
        PersonalDetail *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
        if (user.nickname) {
            [self performSegueWithIdentifier:@"PushPeopleCenter" sender:nil];
        }else{
            [self presentLoginViewController];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    if ([segue.identifier isEqualToString:@"PushPeopleCenter"]) {
//            NSData *userData = [KStandUserDefaults objectForKey:KUserDefaultKey_PersonalDetail];
//            PersonalDetail *user = [NSKeyedUnarchiver unarchiveObjectWithData:userData];
//            if (user.nickname) {
                PeopleCenterTableViewController *tvcPeopleCenter = segue.destinationViewController;
                tvcPeopleCenter.isPush = YES;
//            }else{
//                PeopleCenterTableViewController *tvcPeopleCenter = segue.destinationViewController;
//                tvcPeopleCenter.isPush = NO;
//                [self presentLoginViewController];
//            }
    } 
}

#pragma mark - IsJumpToLoginViewController
-(void)presentLoginViewController
{
    LoginCityStarViewViewController *loginCityStarVC = [[LoginCityStarViewViewController alloc]init];
    BaseNavController *baseloginNav = [[BaseNavController alloc] initWithRootViewController:loginCityStarVC];
    [self presentViewController:baseloginNav animated:YES completion:^{
    }];
}

@end
